package com.sunwayworld.basemodule.common.utils;


import com.sunwayworld.framework.utils.ArithUtils;
import com.sunwayworld.framework.utils.ObjectUtils;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.Set;

public class ConditionUtils {

    /**
     * 采样日期-出生日期 / 365
     * @param collectTime
     * @Param birthday
     * @return
     */
    public static Double getAgeValue(LocalDate collectTime,LocalDate birthday) {
        if (!ObjectUtils.isEmpty(collectTime) && !ObjectUtils.isEmpty(birthday)){
            String ageVal = String.valueOf(collectTime.toEpochDay() - birthday.toEpochDay());
            if (ageVal.equals("0")){
                return 0d;
            }else{
                return new BigDecimal(ArithUtils.div(ageVal,"365")).setScale(2, 4).doubleValue();
            }
        }else{
            return 0d;
        }
    }

    /**
     * @param low    低限
     * @param high   高限
     * @param currentValue   比较值
     * @param flag   判定标识
     */
    public static void getEvalValue(String low, String high, Double currentValue,Set<Boolean> flag,String... referRange){
        Double lowValue = Double.MIN_VALUE;
        Double highValue = Double.MIN_VALUE;
        String originHigh = high;//保存原始值
        StringBuilder sb = new StringBuilder();

        if (low.contains(">")){
            low = low.replace(">", "");
            sb.append(low).append("-");
            lowValue = Double.parseDouble(low) + lowValue;
        }else if (low.contains("≥")){
            low = low.replace("≥", "");
            sb.append(low).append("-");
            lowValue = Double.parseDouble(low);
        }else if (low.contains("-∞")){
            sb.append(low).append("-");
            lowValue = -Double.MAX_VALUE;
        }else{
            sb.append(low).append("-");
            lowValue = Double.parseDouble(low);
        }

        if (high.contains("<")){
            high = high.replace("<", "");
            sb.append(high);
            highValue = Double.parseDouble(high) - highValue;
        }else if (high.contains("≤")){
            high = high.replace("≤", "");
            sb.append(high);
            highValue = Double.parseDouble(high);
        }else if (high.contains("+∞")){
            sb.append(high);
            highValue = Double.MAX_VALUE;
        }else{
            sb.append(high);
            highValue = Double.parseDouble(high);
        }
        if (referRange.length > 0) {
            referRange[0] = sb.length()>0?sb.toString():null;
        }
        if (originHigh.contains("≤")){
            flag.add(lowValue<=currentValue && highValue>=currentValue);
        }else{
            flag.add(lowValue<=currentValue && highValue>currentValue);
        }

    }

    /**
     * @param low    低限
     * @param high   高限
     * @param currentValue   比较值
     * @param flag   判定标识
     */
    public static void getEvalValue(String low, String high, Double currentValue, Set<Boolean> flag, List<Integer> highOrLow, String... referRange){
        Double lowValue = Double.MIN_VALUE;
        Double highValue = Double.MIN_VALUE;
        String originHigh = high;//保存原始值
        StringBuilder sb = new StringBuilder();

        if (low.contains(">")){
            low = low.replace(">", "");
            sb.append(low).append("-");
            lowValue = Double.parseDouble(low) + lowValue;
        }else if (low.contains("≥")){
            low = low.replace("≥", "");
            sb.append(low).append("-");
            lowValue = Double.parseDouble(low);
        }else if (low.contains("-∞")){
            sb.append(low).append("-");
            lowValue = -Double.MAX_VALUE;
        }else{
            sb.append(low).append("-");
            lowValue = Double.parseDouble(low);
        }

        if (high.contains("<")){
            high = high.replace("<", "");
            sb.append(high);
            highValue = Double.parseDouble(high) - highValue;
        }else if (high.contains("≤")){
            high = high.replace("≤", "");
            sb.append(high);
            highValue = Double.parseDouble(high);
        }else if (high.contains("+∞")){
            sb.append(high);
            highValue = Double.MAX_VALUE;
        }else{
            sb.append(high);
            highValue = Double.parseDouble(high);
        }
        if (referRange.length > 0) {
            referRange[0] = sb.length() > 0 ? sb.toString() : null;
        }
        if (originHigh.contains("≤")) {
            if (lowValue > currentValue) {
                highOrLow.add(1);
            } else if (currentValue > highValue) {
                highOrLow.add(2);
            }
            flag.add(lowValue <= currentValue && highValue >= currentValue);
        } else {
            if (lowValue > currentValue) {
                highOrLow.add(1);
            } else if (currentValue >= highValue) {
                highOrLow.add(2);
            }
            flag.add(lowValue <= currentValue && highValue > currentValue);
        }

    }

    /**
     * 不合格判定
     * @param low
     * @param high
     * @param currentValue
     * @return flag    判定结果
     */
    public static String getEvalValue(String low, String high, Double currentValue) {
        Double lowValue = Double.MIN_VALUE;
        Double highValue = Double.MIN_VALUE;

        if (low.contains(">")){
            low = low.replace(">", "");
            lowValue = Double.parseDouble(low) + lowValue;
        }else if (low.contains("≥")){
            low = low.replace("≥", "");
            lowValue = Double.parseDouble(low);
        }else if (low.contains("-∞")){
            lowValue = -Double.MAX_VALUE;
        }else{
            lowValue = Double.parseDouble(low);
        }

        if (high.contains("<")){
            high = high.replace("<", "");
            highValue = Double.parseDouble(high) - highValue;
        }else if (high.contains("≤")){
            high = high.replace("≤", "");
            highValue = Double.parseDouble(high);
        }else if (high.contains("+∞")){
            highValue = Double.MAX_VALUE;
        }else{
            highValue = Double.parseDouble(high);
        }

        String flag = null;
        //偏低
        if (lowValue>currentValue){
            flag = "↓";
        }
        //偏高
        if (highValue<currentValue){
            flag = "↑";
        }

        return flag;
    }

    /**
     *
     * @param value   参考值
     * @param currentValue   比较值
     * @param flag    判定标识
     */
    public static void getEvalValue(String value,String currentValue, Set<Boolean> flag,String... referRange){
        if (referRange.length > 0) {
            referRange[0] = currentValue;
        }
        flag.add(currentValue.equals(value));
    }

    public static double getValue(String value) {
        Double s = Double.MIN_VALUE;

        if (value.contains(">")){
            value = value.replace(">", "");
            s = Double.parseDouble(value) + s;
        }else if (value.contains("≥")){
            value = value.replace("≥", "");
            s = Double.parseDouble(value);
        }else if (value.contains("-∞")){
            s = -Double.MAX_VALUE;
        }else if (value.contains("<")){
            value = value.replace("<", "");
            s = Double.parseDouble(value) - s;
        }else if (value.contains("≤")){
            value = value.replace("≤", "");
            s = Double.parseDouble(value)+s;
        }else if (value.contains("+∞")){
            s = Double.MAX_VALUE;
        } else{
            s = Double.parseDouble(value);
        }

        return s;
    }

    public static boolean compare(double number, String value) {
        Double s = Double.MIN_VALUE;

        if (value.contains(">")){
            value = value.replace(">", "");
            s = Double.parseDouble(value);
            return number > s;
        }else if (value.contains("≥")){
            value = value.replace("≥", "");
            s = Double.parseDouble(value);
            return number >= s;
        }else if (value.contains("-∞")){
            s = -Double.MAX_VALUE;
            return number > s;
        }else if (value.contains("<")){
            value = value.replace("<", "");
            s = Double.parseDouble(value);
            return number < s;
        }else if (value.contains("≤")){
            value = value.replace("≤", "");
            s = Double.parseDouble(value);
            return number <= s;
        }else if (value.contains("+∞")){
            s = Double.MAX_VALUE;
            return number < s;
        }

        return false;
    }

    /**
     * 获取参考值
     * @param referValue
     * @param referRange
     */
    public static void getEvalValue(String referValue,String... referRange){
        if (referRange.length > 0) {
            referRange[0] = referValue;
        }
    }

    /**
     * 获取参考值
     * @param low
     * @param high
     * @param referRange
     */
    public static void getEvalValue(String low, String high,String... referRange){

        StringBuilder sb = new StringBuilder();

        if (low.contains("-∞") || high.contains("+∞")){
            if (low.contains("-∞") && !high.contains("+∞")){
                sb.append(high);
            }else if (!low.contains("-∞") && high.contains("+∞")){
                sb.append(low);
            }
        }else{
            if (low.contains(">")){
                low = low.replace(">", "");
                sb.append(low).append("-");
            }else if (low.contains("≥")){
                low = low.replace("≥", "");
                sb.append(low).append("-");
            }else{
                sb.append(low).append("-");
            }

            if (high.contains("<")){
                high = high.replace("<", "");
                sb.append(high);
            }else if (high.contains("≤")){
                high = high.replace("≤", "");
                sb.append(high);
            }else if (high.contains("+∞")){
                sb.append(high);
            }else{
                sb.append(high);
            }
        }

        if (referRange.length > 0) {
            referRange[0] = sb.length()>0?sb.toString():null;
        }
    }
}
